<html>
<head>
<title>detect collusion</title>
<link rel="icon" type="image/png" href="/favicon.png">
<script>
var display;

var baseurl;
var targets;
var current = 0;
var match_count = 0;
var timeout_index;

var ff_baseurl = "resource://jid1-f9uj2thwoam5gq-at-jetpack/";
var ff_targets = ["lib/main.js", "data/index-content-script.js","collusion/lib/main.js"];
var chrome_baseurl = "chrome-extension://ganlifbpkcplnldliibcbegplfmcfigp/"
var chrome_targets = ["content.js", "d3.js", "scripts/content.js", "scripts/vendor/d3.js"];

function log(message) {
// display.innerHTML += (new Date().toGMTString()) + ": " + message + "\n";
 display.innerHTML += message + "\n";
}

function init() {
 display = document.getElementById("display");
 log("starting from: " + document.location);
 log("user agent reported as: " + navigator.userAgent);
 window.onerror = window_error;

 if ('netscape' in window) {
  log("is Firefox");
  baseurl = ff_baseurl;
  targets = ff_targets;
 } else if ('chrome' in window) {
  log("is Chrome");
  baseurl = chrome_baseurl;
  targets = chrome_targets;
 } else {
  log("browser detection problem");
 }
 loadjs();
}

function loadjs() {
 if (current >= targets.length) {
  finished();
  return;
 }
 var target = targets[current];
 current += 1;

 var scr = document.createElement("script");
 scr.type = "text/javascript";
 scr.onerror = js_error;
 scr.onload = js_load;
 scr.async = true;
 scr.src = baseurl + target;
 log("trying: " + scr.src);
 timeout_index = setTimeout(timeout, 250);
 var s = document.getElementsByTagName("script")[0];
 s.parentNode.insertBefore(scr, s);
}

function window_error(emsg, url, line) {
 clearTimeout(timeout_index);
 log("window error: " + emsg + " (" + url+ "):" + line);
 if (emsg.indexOf("Script error.") > -1) {
  match_count += 1
 }
 if (emsg.indexOf("Error loading script") > -1) {
  match_count += 1
 }
}
function timeout() {
 log("timeout");
 loadjs();
}
function js_load(event) {
// log("js loaded");
 clearTimeout(timeout_index);
 match_count += 1
 loadjs();
}
function js_error(event) {
// log("js error");
 clearTimeout(timeout_index);
 loadjs();
}
function finished() {
 log("finished");
 setTimeout(report, 500);
}
function report() {
 if (match_count > 1) {
  log("Collusion *was* detected!!!");
 } else {
  log("Collusion not detected.");
 }
}
</script>
<body onload="init()">
<div id="display" style="white-space: pre; font-size: x-large; font-family: courier new"></div>

</body>
</html>
